package com.nuc.wxh.vaccine_appointment.vaccines.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuc.wxh.vaccine_appointment.base.exception.VaccineException;
import com.nuc.wxh.vaccine_appointment.base.listener.VaccineListener;
import com.nuc.wxh.vaccine_appointment.base.result.ResultCodeEnum;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.AllVaccine;

import com.nuc.wxh.vaccine_appointment.vaccines.entity.vo.AllVaccineVo;
import com.nuc.wxh.vaccine_appointment.vaccines.mapper.AllVaccineMapper;
import com.nuc.wxh.vaccine_appointment.vaccines.service.AllVaccineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.io.VFS;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * <p>
 * 疫苗信息登记 服务实现类
 * </p>
 *
 * @author 王鑫浩
 * @since 2021-03-10
 */
@Service
public class AllVaccineServiceImpl extends ServiceImpl<AllVaccineMapper, AllVaccine> implements AllVaccineService {

    @Override
    public Page<AllVaccine> selectPage(Page<AllVaccine> pageParam, AllVaccineVo vaccineVo) {
        //条件构造器
        QueryWrapper<AllVaccine> wrapper = new QueryWrapper<>();
        //普通的排序分页查询
        if (vaccineVo == null) {
            return baseMapper.selectPage(pageParam, wrapper);
        }
        //获取对象参数
        String vaccineName = vaccineVo.getVaccineName();
        String vaccineManufacturer = vaccineVo.getVaccineManufacturer();
        String vaccineClassification = vaccineVo.getVaccineClassification();
        //条件
        if (!StringUtils.isEmpty(vaccineName)) {
            wrapper.likeRight("vaccine_name", vaccineName);
        }
        if (!StringUtils.isEmpty(vaccineManufacturer)) {
            wrapper.likeRight("vaccine_manufacturer", vaccineManufacturer);
        }
        if (!StringUtils.isEmpty(vaccineClassification)) {
            wrapper.likeRight("vaccine_classification", vaccineClassification);
        }

        return baseMapper.selectPage(pageParam, wrapper);
    }

    @Override
    public void saveExcel(MultipartFile file, AllVaccineService vaccineService) {
        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in,AllVaccine.class, new VaccineListener(vaccineService)).sheet().doRead();
        } catch (Exception e) {
            throw new VaccineException(ResultCodeEnum.EXCEL_DATA_IMPORT_ERROR);
        }
    }
}
